/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 98152
 * Date: 2024-06-03
 * Time: 16:57
 */
import java.util.concurrent.*;

class Responder implements Runnable {
    private final String name;
    private final CountDownLatch latch;

    public Responder(String name, CountDownLatch latch) {
        this.name = name;
        this.latch = latch;
    }

    public void run() {
        try {
            // 抢答者等待抢答指令
            latch.await();
            // 模拟抢答过程
            Thread.sleep((long) (Math.random() * 1000));
            // 如果抢答成功，则输出成功提示，否则输出失败提示
            if (Math.random() < 0.3) {
                System.out.println(name + "抢答成功！");
            } else {
                System.out.println(name + "未抢答成功。");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class Main1 {
    public static void main(String[] args) {
        int numResponders = 3;
        CountDownLatch latch = new CountDownLatch(1);
        ExecutorService executor = Executors.newFixedThreadPool(numResponders);

        // 创建并启动抢答者线程
        for (int i = 1; i <= numResponders; i++) {
            executor.submit(new Responder("抢答者" + i, latch));
        }

        // 发出抢答指令
        System.out.println("抢答开始！");
        latch.countDown(); // 计数器减一，所有抢答者同时开始抢答

        // 关闭线程池
        executor.shutdown();
    }
}
